import request from '@/utils/request'
let VITE_APP_BASE_API = localStorage.getItem("local_server_ip") ||  window.HC_config && window.HC_config.VITE_APP_BASE_API;
const api = {
    basepath: '/attachment'
}

export function openDownload(parameter) {
    return request({
        url: api.basepath + '/api/download',
        method: 'get',
        params: parameter
    })
}

export function getOpenDownloadUrl(unid) {
    return VITE_APP_BASE_API + api.basepath + '/api/downloadUnid?unid=' + unid
        // return window.Define.PROJECT[window.Define.DEFAULT_PROJECT].VUE_APP_API_BASE_URL + api.basepath + '/api/downloadFile?fileName=' + unid
}


/**
 * 获取word文档预览的pdf地址
 * @param {*} unid 
 * @returns 
 */
export function getWordPreviewUrl(unid) {
    return VITE_APP_BASE_API + api.basepath + '/api/download/wordToPdf/' + unid;
}

/**
 * 图片静态资源地址
 * @param {*} unid 
 * @returns 
 */
export function getImageStaticUrl(path) {
    path = path.replace("upload/","");
    const origin = window.location.origin;////
    //const origin = 'https://yyzc-xtbg.hbsis.gov.cn:30443';
    //console.log(origin);
    return `${origin}/attachment${path}`;
}

/**
 * 获取DWG文件预览的pdf地址
 * @param {*} unid 
 * @returns 
 */
 export function getDwgToPdfUrl(unid) {
    return VITE_APP_BASE_API + api.basepath + '/api/downloadCad/?unid=' + unid;
}

// ereb手写意见图片
export function getSxyjImageUrl (filePath) {
    return VITE_APP_BASE_API + api.basepath + '/api/imageNotion?fileName=' + encodeURIComponent(filePath)
  }

// 福昕打开pdfofd
export function getOpenOfdDownloadUrl(unid) {
    return VITE_APP_BASE_API + api.basepath + '/api/ofdpdfDownloadUnid?unid=' + unid
}
// 获取公文传输打印份数权限
export function getPrintPermission(parameter) {
    return request({
        url: '/gwcs/getPrintPermission',
        method: 'get',
        params: parameter
    })
}
// 公文传输福昕打印后回调
export function uploadPrintResult(parameter) {
    return request({
        url: '/gwcs/uploadPrintResult',
        method: 'get',
        params: parameter
    })
}
/**
 * 
 * @param { String } fileName 文件路径
 * @param { boolean | undefined } isEditor  是否是富文本编辑
 * @returns 
 */
export function getOpenDownloadFile(fileName,isEditor) {
    let base = isEditor ? (window.HC_config.VUE_APP_API_EXTERNAL || VITE_APP_BASE_API) : VITE_APP_BASE_API;
    return base + api.basepath + '/api/downloadFile?fileName=' + fileName;
}

export function getOpenFilePath(path) {
    return VITE_APP_BASE_API + '/profile/upload/' + encodeURI(path)
}

export function getOpenList(parameter) {
    return request({
        url: api.basepath + '/api/list',
        method: 'get',
        params: parameter
    })
}

export function getNewAttachment(parameter) {
    return request({
        url: api.basepath + '/new',
        method: 'get',
        params: parameter
    })
}

export function openUpload(formData, callback) {
    console.log(formData.get('file'))
    return request({
        url: api.basepath + '/api/upload',
        method: 'post',
        data: formData,
        timeout: 10000000,
        headers:{
            'Content-Type':'multipart/form-data',
            'repeatSubmit':false
        },
        onUploadProgress(progressEvent) {
            if (progressEvent.lengthComputable) {
                const val = (progressEvent.loaded / progressEvent.total * 100).toFixed(0)
                var progress = parseInt(val)
                console.log(progress)
                callback(progress)
            }
        }
    })
}
export function openUpload2(formData, callback) {
    let url = formData.url ? JSON.parse(JSON.stringify(formData.url)) : ""
    delete formData.url
    return request({
        url: url ? url : api.basepath + '/api/upload',
        method: 'post',
        data: formData,
        timeout: 10000000,
        onUploadProgress(progressEvent) {
            if (progressEvent.lengthComputable) {
                const val = (progressEvent.loaded / progressEvent.total * 100).toFixed(0)
                var progress = parseInt(val)
                callback(progress)
            }
        }
    })
}
export function saveAttachment(parameter) {
    return request({
        url: api.basepath,
        method: 'post',
        data: parameter,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        }
    })
}
export function saveAttachmentApi(parameter) {
    return request({
        url: api.basepath + '/api',
        method: 'post',
        data: parameter,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        }
    })
}
export function delAttachment(parameter) {
    return request({
        url: api.basepath,
        method: 'Delete',
        params: parameter
    })
}

export function getList(parameter) {
    return request({
        url: api.basepath + '/list',
        method: 'get',
        params: parameter
    })
}

export function getInfo(unid) {
    return request({
        url: api.basepath + '/' + unid,
        method: 'get'
    })
}

/**
 * cs 2023-6-20
 * 附件排序
 */
export function attachmentRank(data)
{
    return request({
        url: api.basepath + '/attachmentRank',
        method: "post",
        data
    })
}